home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  35.1 KB  |  1,223 lines

  1. /* Copyright (C) 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpm.c,v 1.2 2000/09/19 19:00:20 lpd Exp $ */
  20. /*
  21.  * OS/2 Presentation manager driver
  22.  *
  23.  * By Russell Lang (based on gdevmswn.c and gdevwdib.c)
  24.  *
  25.  * If Ghostscript is a PM application, stdin/stdout are not
  26.  * provided and so no text window is available.
  27.  * If Ghostscript is a windowed text application, a message queue
  28.  * can't be created so a PM window for graphics can't be created.
  29.  * The solution used here is to have two programs - gsos2.exe is a 
  30.  * text application and the outboard PM driver is gspmdrv.exe.
  31.  * Another solution may be to make Ghostscript a PM application
  32.  * and use VIO calls to provide a text window.
  33.  *
  34.  * If PM GSview starts Ghostscript, PM GSview displays the
  35.  * bitmap instead of the PM driver (gspmdrv.exe).
  36.  *
  37.  * Since Ghostscript is not a PM application, this driver creates a
  38.  * BMP bitmap in a named shared memory block and a second process
  39.  * gspmdrv.exe reads this memory block and provides the PM window.  
  40.  * Communication to gspmdrv.exe is via the shared memory block
  41.  * and semaphores.
  42.  */
  43.  
  44. #define INCL_DOS
  45. #define INCL_DOSERRORS
  46. #define INCL_WINWINDOWMGR
  47. #define INCL_DEV
  48. #define INCL_GPIBITMAPS
  49.  
  50. #include <os2.h>
  51. #include "string_.h"
  52. #include <stdlib.h>
  53. #include "gx.h"
  54. #include "gserrors.h"
  55. #include "gsexit.h"        /* for gs_exit_status */
  56. #include "gxdevice.h"
  57.  
  58. #include "gp.h"
  59. #include "gpcheck.h"
  60. #include "gsparam.h"
  61. #include "gdevpccm.h"
  62.  
  63. #include "gxdevmem.h"
  64. #include "gdevpm.h"
  65. #ifdef __DLL__
  66. #include "gsdll.h"
  67. #include "gsdllos2.h"
  68. #endif
  69.  
  70. #define MIN_COMMIT 4096        /* memory is committed in these size chunks */
  71. #define ID_NAME "GSPMDRV_%u_%u"
  72.  
  73. /* Initial values for width and height */
  74. #define INITIAL_RESOLUTION 96
  75. #define INITIAL_WIDTH (INITIAL_RESOLUTION * 85 / 10 + 1)
  76. #define INITIAL_HEIGHT (INITIAL_RESOLUTION * 11 + 1)
  77.  
  78. /* A macro for casting the device argument */
  79. #define pmdev ((gx_device_pm *)dev)
  80.  
  81.  
  82. #define pm_gsview_sizeof 80
  83. typedef struct gx_device_pm_s gx_device_pm;
  84.  
  85. #define gx_device_pm_common\
  86.     int BitsPerPixel;\
  87.     int UpdateInterval;\
  88.     char GSVIEW[pm_gsview_sizeof];\
  89.     BOOL dll;\
  90.     int nColors;\
  91.     BOOL updating;\
  92.     HTIMER update_timer;\
  93.     HEV sync_event;\
  94.     HEV next_event;\
  95.     HMTX bmp_mutex;\
  96.     HQUEUE drv_queue;\
  97.     HQUEUE term_queue;\
  98.     ULONG session_id;\
  99.     PID process_id;\
  100.     PID gspid;\
  101.     unsigned char *bitmap;\
  102.     ULONG committed;\
  103.     PBITMAPINFO2 bmi
  104.  
  105. /* The device descriptor */
  106. struct gx_device_pm_s {
  107.     gx_device_common;
  108.     gx_device_pm_common;
  109.     gx_device_memory mdev;
  110. };
  111.  
  112. /* Device procedures */
  113.  
  114. /* See gxdevice.h for the definitions of the procedures. */
  115. private dev_proc_open_device(pm_open);
  116. private dev_proc_get_initial_matrix(pm_get_initial_matrix);
  117. private dev_proc_sync_output(pm_sync_output);
  118. private dev_proc_output_page(pm_output_page);
  119. private dev_proc_close_device(pm_close);
  120. private dev_proc_map_rgb_color(pm_map_rgb_color);
  121. private dev_proc_map_color_rgb(pm_map_color_rgb);
  122. private dev_proc_fill_rectangle(pm_fill_rectangle);
  123. private dev_proc_copy_mono(pm_copy_mono);
  124. private dev_proc_copy_color(pm_copy_color);
  125. private dev_proc_get_bits(pm_get_bits);
  126. private dev_proc_get_params(pm_get_params);
  127. private dev_proc_put_params(pm_put_params);
  128.  
  129. private gx_device_procs pm_procs =
  130. {
  131.     pm_open,
  132.     pm_get_initial_matrix,
  133.     pm_sync_output,
  134.     pm_output_page,
  135.     pm_close,
  136.     pm_map_rgb_color,
  137.     pm_map_color_rgb,
  138.     pm_fill_rectangle,
  139.     NULL,            /* tile rectangle */
  140.     pm_copy_mono,
  141.     pm_copy_color,
  142.     NULL,            /* draw line */
  143.     pm_get_bits,
  144.     pm_get_params,
  145.     pm_put_params,
  146.     NULL,            /* map_cmyk_color */
  147.     gx_default_get_xfont_procs,
  148.     NULL,            /* get_xfont_device */
  149.     NULL,            /* map_rgb_alpha_color */
  150.     gx_page_device_get_page_device
  151. };
  152.  
  153. #ifdef __DLL__
  154. gx_device_pm far_data gs_os2dll_device =
  155. {
  156.     std_device_std_body(gx_device_pm, &pm_procs, "os2dll",
  157.             INITIAL_WIDTH, INITIAL_HEIGHT,
  158.             INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  159.     {0},            /* std_procs */
  160.     8,                /* BitsPerPixel */
  161.     5000,            /* UpdateInterval */
  162.     "\0",            /* GSVIEW */
  163.     1                /* is DLL device */
  164. };
  165.  
  166. #endif
  167. gx_device_pm far_data gs_os2pm_device =
  168. {
  169.     std_device_std_body(gx_device_pm, &pm_procs, "os2pm",
  170.             INITIAL_WIDTH, INITIAL_HEIGHT,
  171.             INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  172.     {0},            /* std_procs */
  173.     8,                /* BitsPerPixel */
  174.     5000,            /* UpdateInterval */
  175.     "\0",            /* GSVIEW */
  176.     0                /* is not DLL device */
  177. };
  178.  
  179. /* Compress a gx_color_value into an 8-bit PM color value, */
  180. /* using only the high order 5 bits. */
  181. #define pm_color_value(z)\
  182.   ((((z) >> (gx_color_value_bits - 5)) << 3) +\
  183.    ((z) >> (gx_color_value_bits - 3)))
  184.  
  185. /* prototypes for internal procedures */
  186. private void pm_makepalette(gx_device_pm *);
  187. private void pm_update(gx_device_pm *);
  188. private uint pm_set_bits_per_pixel(gx_device_pm *, int);
  189. private uint pm_palette_size(gx_device_pm *);
  190. private int pm_alloc_bitmap(gx_device_pm *, gx_device *);
  191. private int pm_run_gspmdrv(gx_device_pm *);
  192. private void pm_write_bmp(gx_device_pm *);
  193.  
  194. /* Open the pm driver */
  195. int
  196. pm_open(gx_device * dev)
  197. {
  198.     int ccode;
  199.     CHAR id[128];
  200.     CHAR name[128];
  201.     PTIB pptib;
  202.     PPIB pppib;
  203.  
  204.     if (!pmdev->dll && (_osmode == DOS_MODE)) {
  205.     fprintf(stderr, "os2pm driver can't be used under DOS\n");
  206.     return gs_error_limitcheck;
  207.     }
  208.     if (DosGetInfoBlocks(&pptib, &pppib)) {
  209.     fprintf(stderr, "\npm_open: Couldn't get pid\n");
  210.     return gs_error_limitcheck;
  211.     }
  212. #ifdef __DLL__
  213.     if (pppib->pib_ultype == 3)    /* if caller is PM app */
  214.     pmdev->gspid = pppib->pib_ulpid;    /* use caller pid */
  215.     else
  216. #endif
  217.     pmdev->gspid = pppib->pib_ulppid;    /* use parent (CMD.EXE) pid */
  218.     sprintf(id, ID_NAME, pmdev->gspid, (ULONG) dev);
  219.  
  220.     /* Allocate, but don't commit, enough memory for the largest */
  221.     /* possible bitmap (13Mbytes = A3 x 150dpi x 24bits) */
  222. #ifdef __DLL__
  223.     if (pmdev->dll) {
  224.     /* We don't need to use shared memory for the DLL */
  225.     if (DosAllocMem((PPVOID) & pmdev->bitmap,
  226.             13 * 1024 * 1024, PAG_READ | PAG_WRITE)) {
  227.         fprintf(stderr, "pm_open: failed allocating BMP memory\n");
  228.         return gs_error_limitcheck;
  229.     }
  230.     } else
  231. #endif
  232.     {
  233.     /* Shared memory is common to all processes so we don't want to allocate too much */
  234.     sprintf(name, SHARED_NAME, *pmdev->GSVIEW ? pmdev->GSVIEW : id);
  235.     if (DosAllocSharedMem((PPVOID) & pmdev->bitmap, name,
  236.                   13 * 1024 * 1024, PAG_READ | PAG_WRITE)) {
  237.         fprintf(stderr, "pm_open: failed allocating shared BMP memory %s\n", name);
  238.         return gs_error_limitcheck;
  239.     }
  240.     }
  241.  
  242.     /* commit one page so there is enough storage for a */
  243.     /* bitmap header and palette */
  244.     if (DosSetMem(pmdev->bitmap, MIN_COMMIT, PAG_COMMIT | PAG_DEFAULT)) {
  245.     DosFreeMem(pmdev->bitmap);
  246.     fprintf(stderr, "pm_open: failed committing BMP memory\n");
  247.     return gs_error_limitcheck;
  248.     }
  249.     pmdev->committed = MIN_COMMIT;
  250.  
  251.     if (pmdev->dll) {
  252.     /* Create mutex - used for preventing another thread from accessing */
  253.     /* bitmap while we are changing the bitmap size. Initially unowned. */
  254.     sprintf(name, MUTEX_NAME, id);
  255.     if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  256.         DosFreeMem(pmdev->bitmap);
  257.         DosCloseEventSem(pmdev->sync_event);
  258.         DosCloseQueue(pmdev->drv_queue);
  259.         fprintf(stderr, "pm_open: failed to create mutex semaphore %s\n", name);
  260.         return gs_error_limitcheck;
  261.     }
  262.     } else {
  263.     if (*pmdev->GSVIEW) {
  264.         APIRET rc;
  265.  
  266.         /* GSview has already created the necessary objects */
  267.         /* so we use Open instead of Create */
  268.         rc = 0;
  269.         if (!rc) {
  270.         sprintf(name, NEXT_NAME, pmdev->GSVIEW);
  271.         rc = DosOpenEventSem(name, &pmdev->next_event);
  272.         }
  273.         if (!rc) {
  274.         sprintf(name, MUTEX_NAME, pmdev->GSVIEW);
  275.         rc = DosOpenMutexSem(name, &pmdev->bmp_mutex);
  276.         }
  277.         if (!rc) {
  278.         PID owner_pid;
  279.  
  280.         sprintf(name, QUEUE_NAME, pmdev->GSVIEW);
  281.         rc = DosOpenQueue(&owner_pid, &pmdev->drv_queue, name);
  282.         }
  283.         if (rc) {
  284.         DosFreeMem(pmdev->bitmap);
  285.         DosCloseEventSem(pmdev->next_event);
  286.         fprintf(stderr, "pm_open: failed to open %s, rc = %u\n", name, rc);
  287.         return gs_error_limitcheck;
  288.         }
  289.     } else {        /* not GSVIEW */
  290.         /* Create update event semaphore */
  291.         sprintf(name, SYNC_NAME, id);
  292.         if (DosCreateEventSem(name, &(pmdev->sync_event), 0, FALSE)) {
  293.         DosFreeMem(pmdev->bitmap);
  294.         fprintf(stderr, "pm_open: failed to create event semaphore %s\n", name);
  295.         return gs_error_limitcheck;
  296.         }
  297.         /* Create mutex - used for preventing gspmdrv from accessing */
  298.         /* bitmap while we are changing the bitmap size. Initially unowned. */
  299.         sprintf(name, MUTEX_NAME, id);
  300.         if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  301.         DosFreeMem(pmdev->bitmap);
  302.         DosCloseEventSem(pmdev->sync_event);
  303.         DosCloseQueue(pmdev->drv_queue);
  304.         fprintf(stderr, "pm_open: failed to create mutex semaphore %s\n", name);
  305.         return gs_error_limitcheck;
  306.         }
  307.     }
  308.     }
  309.  
  310.     if ((pm_set_bits_per_pixel(pmdev, pmdev->BitsPerPixel) < 0) ||
  311.     (gdev_mem_device_for_bits(dev->color_info.depth) == 0)) {
  312.     if (!pmdev->dll) {
  313.         if (*pmdev->GSVIEW) {
  314.         DosCloseQueue(pmdev->drv_queue);
  315.         DosCloseEventSem(pmdev->next_event);
  316.         } else
  317.         DosCloseEventSem(pmdev->sync_event);
  318.     }
  319.     DosCloseMutexSem(pmdev->bmp_mutex);
  320.     DosFreeMem(pmdev->bitmap);
  321.     return gs_error_limitcheck;
  322.     }
  323.     /* initialise bitmap header */
  324.     pmdev->bmi = (PBITMAPINFO2) pmdev->bitmap;
  325.     pmdev->bmi->cbFix = 40;    /* OS/2 2.0 and Windows 3.0 compatible */
  326.     pmdev->bmi->cx = dev->width;
  327.     pmdev->bmi->cy = dev->height;
  328.     pmdev->bmi->cPlanes = 1;
  329.     pmdev->bmi->cBitCount = dev->color_info.depth;
  330.     pmdev->bmi->ulCompression = BCA_UNCOMP;
  331.     pmdev->bmi->cbImage = 0;
  332.     pmdev->bmi->cxResolution = (ULONG) (dev->x_pixels_per_inch / 25.4 * 1000);
  333.     pmdev->bmi->cyResolution = (ULONG) (dev->y_pixels_per_inch / 25.4 * 1000);
  334.     if (pmdev->BitsPerPixel <= 8) {
  335.     pmdev->bmi->cclrUsed = 1 << (pmdev->BitsPerPixel);
  336.     pmdev->bmi->cclrImportant = pmdev->nColors;
  337.     } else {
  338.     pmdev->bmi->cclrUsed = 0;
  339.     pmdev->bmi->cclrImportant = 0;
  340.     }
  341.  
  342.     pm_makepalette(pmdev);
  343.  
  344.     /* commit pages */
  345.     ccode = pm_alloc_bitmap((gx_device_pm *) dev, dev);
  346.     if (ccode < 0) {
  347.     if (!pmdev->dll) {
  348.         if (*pmdev->GSVIEW) {
  349.         DosCloseQueue(pmdev->drv_queue);
  350.         DosCloseEventSem(pmdev->next_event);
  351.         } else
  352.         DosCloseEventSem(pmdev->sync_event);
  353.     }
  354.     DosCloseMutexSem(pmdev->bmp_mutex);
  355.     DosFreeMem(pmdev->bitmap);
  356.     return ccode;
  357.     }
  358.     if (*pmdev->GSVIEW)
  359.     return 0;        /* GSview will handle displaying */
  360.  
  361. #ifdef __DLL__
  362.     if (pmdev->dll) {
  363.     /* notify caller about new device */
  364.     (*pgsdll_callback) (GSDLL_DEVICE, (unsigned char *)pmdev, 1);
  365.     return 0;        /* caller will handle displaying */
  366.     }
  367. #endif
  368.  
  369.     ccode = pm_run_gspmdrv(pmdev);
  370.     if (ccode < 0) {
  371.     DosFreeMem(pmdev->bitmap);
  372.     DosCloseEventSem(pmdev->sync_event);
  373.     DosCloseMutexSem(pmdev->bmp_mutex);
  374.     }
  375.     return ccode;
  376. }
  377.  
  378. /* Get the initial matrix.  BMPs, unlike most displays, */
  379. /* put (0,0) in the lower left corner. */
  380. private void
  381. pm_get_initial_matrix(gx_device * dev, gs_matrix * pmat)
  382. {
  383.     pmat->xx = dev->x_pixels_per_inch / 72.0;
  384.     pmat->xy = 0;
  385.     pmat->yx = 0;
  386.     pmat->yy = dev->y_pixels_per_inch / 72.0;
  387.     pmat->tx = 0;
  388.     pmat->ty = 0;
  389.     if (*pmdev->GSVIEW)
  390.     pm_update((gx_device_pm *) dev);    /* let GSVIEW know we are drawing */
  391. }
  392.  
  393. /* Make the output appear on the screen. */
  394. int
  395. pm_sync_output(gx_device * dev)
  396. {
  397. #ifdef __DLL__
  398.     if (pmdev->dll) {
  399.     (*pgsdll_callback) (GSDLL_SYNC, (unsigned char *)dev, 0);
  400.     return 0;
  401.     }
  402. #endif
  403.  
  404.     /* tell gspmdrv or GSview process to update display */
  405.     if (*pmdev->GSVIEW) {
  406.     APIRET rc;
  407.  
  408.     rc = DosWriteQueue(pmdev->drv_queue, GS_SYNC, 0, NULL, 0);
  409.     if (rc)
  410.         fprintf(stderr, "pm_sync_output: DosWriteQueue error %d\n", rc);
  411.     } else {
  412.     if (pmdev->updating)
  413.         DosStopTimer(pmdev->update_timer);
  414.     DosPostEventSem(pmdev->sync_event);
  415.     }
  416.     pmdev->updating = FALSE;
  417.     return (0);
  418. }
  419.  
  420. /* Make the output appear on the screen  */
  421. /* and bring image window to foreground. */
  422. private int
  423. pm_do_output_page(gx_device * dev, int copies, int flush)
  424. {
  425.     int code;
  426.     APIRET rc;
  427.  
  428. #ifdef DEBUG
  429.     pm_write_bmp(pmdev);
  430. #endif
  431. #ifdef __DLL__
  432.     if (pmdev->dll) {
  433.     (*pgsdll_callback) (GSDLL_PAGE, (unsigned char *)dev, 0);
  434.     return 0;
  435.     }
  436. #endif
  437.  
  438.     if (*pmdev->GSVIEW) {
  439.     if (copies == -2) {
  440.         rc = DosWriteQueue(pmdev->drv_queue, GS_END, 0, NULL, 0);
  441.         if (rc)
  442.         fprintf(stderr, "pm_output_page: DosWriteQueue error %d\n", rc);
  443.     } else if (copies == -1) {
  444.         rc = DosWriteQueue(pmdev->drv_queue, GS_BEGIN, 0, NULL, 0);
  445.         if (rc)
  446.         fprintf(stderr, "pm_output_page: DosWriteQueue error %d\n", rc);
  447.     } else {
  448.         ULONG count;
  449.  
  450.         pmdev->updating = FALSE;
  451.         /* signal GSview that another page is ready */
  452.         rc = DosWriteQueue(pmdev->drv_queue, GS_PAGE, 0, NULL, 0);
  453.         if (rc)
  454.         fprintf(stderr, "pm_output_page: DosWriteQueue error %d\n", rc);
  455.         /* wait for GSview to signal we can move on to next page */
  456.         DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  457.         DosResetEventSem(pmdev->next_event, &count);
  458.     }
  459.     code = 0;
  460.     } else {
  461.     code = pm_sync_output(dev);
  462.     rc = DosSelectSession(pmdev->session_id);
  463.     if (rc) {
  464.         DosSleep(2000);    /* give gspmdrv.exe a chance to run */
  465.         rc = DosSelectSession(pmdev->session_id);
  466.         if (rc == ERROR_SMG_NO_TARGET_WINDOW) {
  467.         DosSleep(5000);    /* give gspmdrv.exe a chance to run */
  468.         rc = DosSelectSession(pmdev->session_id);    /* try yet again */
  469.         }
  470.         if ((rc == ERROR_SMG_SESSION_NOT_FOUND) ||
  471.         (rc == ERROR_SMG_INVALID_SESSION_ID)) {
  472.         /* someone has killed the session */
  473.         REQUESTDATA Request;
  474.         ULONG DataLength;
  475.         PVOID DataAddress;
  476.         PULONG QueueEntry;
  477.         BYTE ElemPriority;
  478.  
  479.         /* Close gspmdrv driver */
  480.         DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  481.         Request.pid = pmdev->gspid;
  482.         Request.ulData = 0;
  483.         /* wait for termination queue, queue is then closed by session manager */
  484.         DosReadQueue(pmdev->term_queue, &Request, &DataLength,
  485.              &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV) NULL);
  486.         DosCloseQueue(pmdev->term_queue);
  487.         pmdev->term_queue = (HQUEUE) 0;
  488.         /* restart it */
  489.         pm_run_gspmdrv(pmdev);
  490.         DosSleep(2000);    /* give gspmdrv.exe a chance to run */
  491.         rc = DosSelectSession(pmdev->session_id);
  492.         }
  493.         if (rc == ERROR_SMG_SESSION_NOT_FOREGRND)
  494.         DosBeep(400, 50);
  495.         else if (rc)
  496.         fprintf(stderr, "pm_output_page: Select Session error code %u\n", rc);
  497.     }
  498.     }
  499.     return code;
  500. }
  501. int
  502. pm_output_page(gx_device * dev, int copies, int flush)
  503. {
  504.     int code = pm_do_output_page(dev, copies, flush);
  505.  
  506.     if (code >= 0)
  507.     code = gx_finish_output_page(dev, copies, flush);
  508.     return code;
  509. }
  510.  
  511. /* Close the pm driver */
  512. int
  513. pm_close(gx_device * dev)
  514. {
  515.     APIRET rc;
  516.  
  517. #ifdef __DLL__
  518.     if (pmdev->dll) {
  519.     DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  520.     (*pgsdll_callback) (GSDLL_DEVICE, (unsigned char *)dev, 0);
  521.     DosReleaseMutexSem(pmdev->bmp_mutex);
  522.     } else
  523. #endif
  524.     {
  525.     if (*pmdev->GSVIEW) {
  526.         if (gs_exit_status) {
  527.         ULONG count;
  528.  
  529.         /* pause so error messages can be read */
  530.         DosResetEventSem(pmdev->next_event, &count);
  531.         DosWriteQueue(pmdev->drv_queue, GS_ERROR, 0, NULL, 0);
  532.         DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  533.         DosResetEventSem(pmdev->next_event, &count);
  534.         }
  535.         rc = DosWriteQueue(pmdev->drv_queue, GS_CLOSE, 0, NULL, 0);
  536.         if (rc)
  537.         fprintf(stderr, "pm_close: DosWriteQueue error %d\n", rc);
  538.     } else {
  539.         REQUESTDATA Request;
  540.         ULONG DataLength;
  541.         PVOID DataAddress;
  542.         PULONG QueueEntry;
  543.         BYTE ElemPriority;
  544.  
  545.         /* Close gspmdrv driver */
  546.         DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  547.         Request.pid = pmdev->gspid;
  548.         Request.ulData = 0;
  549.         /* wait for termination queue, queue is then closed by session manager */
  550.         DosReadQueue(pmdev->term_queue, &Request, &DataLength,
  551.              &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV) NULL);
  552.         /* queue needs to be closed by us */
  553.         DosCloseQueue(pmdev->term_queue);
  554.     }
  555.     }
  556.     /* release memory */
  557.     DosFreeMem(pmdev->bitmap);
  558.     pmdev->bitmap = (unsigned char *)NULL;
  559.     pmdev->committed = 0;
  560.  
  561.     if (!pmdev->dll) {
  562.     /* close objects */
  563.     if (*pmdev->GSVIEW) {
  564.         DosCloseQueue(pmdev->drv_queue);
  565.         DosCloseEventSem(pmdev->next_event);
  566.     } else {
  567.         DosCloseEventSem(pmdev->sync_event);
  568.         /* stop update timer */
  569.         if (pmdev->updating)
  570.         DosStopTimer(pmdev->update_timer);
  571.         pmdev->updating = FALSE;
  572.     }
  573.     }
  574.     DosCloseMutexSem(pmdev->bmp_mutex);
  575.     return (0);
  576. }
  577.  
  578.  
  579. /* Map a r-g-b color to the colors available under PM */
  580. gx_color_index
  581. pm_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  582.          gx_color_value b)
  583. {
  584.     switch (dev->color_info.depth) {
  585.     case 24:
  586.         return ((b >> (gx_color_value_bits - 8)) << 16) +
  587.         ((g >> (gx_color_value_bits - 8)) << 8) +
  588.         ((r >> (gx_color_value_bits - 8)));
  589.     case 8:{
  590.         int i;
  591.         RGB2 *prgb;
  592.         byte cr, cg, cb;
  593.  
  594.         /* map colors to 0->255 in 32 steps */
  595.         cr = pm_color_value(r);
  596.         cg = pm_color_value(g);
  597.         cb = pm_color_value(b);
  598.  
  599.         prgb = (RGB2 *) ((PBYTE) pmdev->bmi + pmdev->bmi->cbFix);
  600.         /* search in palette */
  601.         for (i = 0; i < pmdev->nColors; i++, prgb++) {
  602.             if (!((cr ^ prgb->bRed) & 0xf8) &&
  603.             !((cg ^ prgb->bGreen) & 0xf8) &&
  604.             !((cb ^ prgb->bBlue) & 0xf8)
  605.             )
  606.             return ((gx_color_index) i);    /* found it */
  607.         }
  608.  
  609.         /* next try adding it to palette */
  610.         if (i < 230) {    /* allow 26 for PM and other apps */
  611.             prgb->bRed = cr;
  612.             prgb->bGreen = cg;
  613.             prgb->bBlue = cb;
  614.             prgb->fcOptions = 0;
  615.             pmdev->nColors = i + 1;
  616.             pmdev->bmi->cclrImportant = pmdev->nColors;
  617.             if (*pmdev->GSVIEW) {
  618.             APIRET rc;
  619.  
  620.             rc = DosWriteQueue(pmdev->drv_queue, GS_PALCHANGE, 0, NULL, 0);
  621.             if (rc)
  622.                 fprintf(stderr, "pm_sync_output: DosWriteQueue error %d\n", rc);
  623.             }
  624.             return ((gx_color_index) i);    /* return new palette index */
  625.         }
  626.         return (gx_no_color_index);    /* not found - dither instead */
  627.         }
  628.     case 4:
  629.         if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
  630.         && (r < gx_max_color_value / 4 * 3))
  631.         return ((gx_color_index) 8);    /* light gray */
  632.         return pc_4bit_map_rgb_color(dev, r, g, b);
  633.     }
  634.     return (gx_default_map_rgb_color(dev, r, g, b));
  635. }
  636.  
  637. /* Map a color code to r-g-b. */
  638. int
  639. pm_map_color_rgb(gx_device * dev, gx_color_index color,
  640.          gx_color_value prgb[3])
  641. {
  642.     gx_color_value one;
  643.  
  644.     switch (dev->color_info.depth) {
  645.     case 24:
  646.         one = (gx_color_value) (gx_max_color_value / 255);
  647.         prgb[0] = ((color) & 255) * one;
  648.         prgb[1] = ((color >> 8) & 255) * one;
  649.         prgb[2] = ((color >> 16) & 255) * one;
  650.         break;
  651.     case 8:
  652.         if (!dev->is_open)
  653.         return -1;
  654.         {
  655.         RGB2 *argb = (RGB2 *) ((PBYTE) pmdev->bmi + pmdev->bmi->cbFix);
  656.  
  657.         one = (gx_color_value) (gx_max_color_value / 255);
  658.         prgb[0] = argb[(int)color].bRed * one;
  659.         prgb[1] = argb[(int)color].bGreen * one;
  660.         prgb[2] = argb[(int)color].bBlue * one;
  661.         }
  662.         break;
  663.     case 4:
  664.         if (color == 8)    /* VGA light gray */
  665.         prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
  666.         else
  667.         pc_4bit_map_color_rgb(dev, color, prgb);
  668.         break;
  669.     default:
  670.         prgb[0] = prgb[1] = prgb[2] =
  671.         (int)color ? gx_max_color_value : 0;
  672.     }
  673.     return 0;
  674. }
  675.  
  676. #define pmmdev ((gx_device *)&pmdev->mdev)
  677. #define pmmproc(proc) (*dev_proc(&pmdev->mdev, proc))
  678.  
  679. /* Fill a rectangle. */
  680. private int
  681. pm_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  682.           gx_color_index color)
  683. {
  684.     pmmproc(fill_rectangle) (pmmdev, x, y, w, h, color);
  685.     pm_update((gx_device_pm *) dev);
  686.     return 0;
  687. }
  688.  
  689. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  690. /* Color = gx_no_color_index means transparent (no effect on the image). */
  691. private int
  692. pm_copy_mono(gx_device * dev,
  693.          const byte * base, int sourcex, int raster, gx_bitmap_id id,
  694.          int x, int y, int w, int h,
  695.          gx_color_index zero, gx_color_index one)
  696. {
  697.     pmmproc(copy_mono) (pmmdev, base, sourcex, raster, id,
  698.             x, y, w, h, zero, one);
  699.     pm_update((gx_device_pm *) dev);
  700.     return 0;
  701. }
  702.  
  703. /* Copy a color pixel map.  This is just like a bitmap, except that */
  704. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  705. private int
  706. pm_copy_color(gx_device * dev,
  707.           const byte * base, int sourcex, int raster, gx_bitmap_id id,
  708.           int x, int y, int w, int h)
  709. {
  710.     pmmproc(copy_color) (pmmdev, base, sourcex, raster, id,
  711.              x, y, w, h);
  712.     pm_update((gx_device_pm *) dev);
  713.     return 0;
  714. }
  715.  
  716. int
  717. pm_get_bits(gx_device * dev, int y, byte * str, byte ** actual_data)
  718. {
  719.     return pmmproc(get_bits) (pmmdev, y, str, actual_data);
  720. }
  721. /* Get PM parameters */
  722. int
  723. pm_get_params(gx_device * dev, gs_param_list * plist)
  724. {
  725.     int code = gx_default_get_params(dev, plist);
  726.     gs_param_string gvs;
  727.  
  728.     gvs.data = pmdev->GSVIEW, gvs.size = strlen(gvs.data),
  729.     gvs.persistent = false;
  730.     code < 0 ||
  731.     (code = param_write_int(plist, "UpdateInterval", &pmdev->UpdateInterval)) < 0 ||
  732.     (code = param_write_string(plist, "GSVIEW", &gvs)) < 0;
  733.     return code;
  734. }
  735.  
  736. /* Put parameters. */
  737.  
  738. /* Set PM parameters -- size and resolution. */
  739. /* We implement this ourselves so that we can do it without */
  740. /* closing and opening the device. */
  741. /* Also set BitsPerPixel and GSVIEW if device not open */
  742. int
  743. pm_put_params(gx_device * dev, gs_param_list * plist)
  744. {
  745.     int ecode = 0, code;
  746.     bool reopen = false;
  747.     bool is_open = dev->is_open;
  748.     int width = dev->width;
  749.     int height = dev->height;
  750.     int old_bpp = dev->color_info.depth;
  751.     int bpp = old_bpp;
  752.     int uii = pmdev->UpdateInterval;
  753.     gs_param_string gsvs;
  754.  
  755.     /* Handle extra parameters */
  756.     switch (code = param_read_string(plist, "GSVIEW", &gsvs)) {
  757.     case 0:
  758.         if (gsvs.size == strlen(pmdev->GSVIEW) &&
  759.         !memcmp(pmdev->GSVIEW, gsvs.data, gsvs.size)
  760.         ) {
  761.         gsvs.data = 0;
  762.         break;
  763.         }
  764.         if (dev->is_open)
  765.         ecode = gs_error_rangecheck;
  766.         else if (gsvs.size >= pm_gsview_sizeof)
  767.         ecode = gs_error_limitcheck;
  768.         else
  769.         break;
  770.         goto gsve;
  771.     default:
  772.         ecode = code;
  773.       gsve:param_signal_error(plist, "GSVIEW", ecode);
  774.     case 1:
  775.         gsvs.data = 0;
  776.         break;
  777.     }
  778.  
  779.     switch (code = param_read_int(plist, "UpdateInterval", &uii)) {
  780.     case 0:
  781.         if (uii < 0)
  782.         ecode = gs_error_rangecheck;
  783.         else
  784.         break;
  785.         goto uie;
  786.     default:
  787.         ecode = code;
  788.       uie:param_signal_error(plist, "UpdateInterval", ecode);
  789.     case 1:
  790.         break;
  791.     }
  792.  
  793.     switch (code = param_read_int(plist, "BitsPerPixel", &bpp)) {
  794.     case 0:
  795.         if (dev->is_open && bpp != old_bpp)
  796.         ecode = gs_error_rangecheck;
  797.         else {
  798.         code = pm_set_bits_per_pixel(pmdev, bpp);
  799.         if (code < 0)
  800.             ecode = code;
  801.         else
  802.             break;
  803.         }
  804.         goto bppe;
  805.     default:
  806.         ecode = code;
  807.       bppe:param_signal_error(plist, "BitsPerPixel", ecode);
  808.     case 1:
  809.         break;
  810.     }
  811.  
  812.     if (ecode >= 0) {        /* Prevent gx_default_put_params from closing the device. */
  813.     dev->is_open = false;
  814.     ecode = gx_default_put_params(dev, plist);
  815.     dev->is_open = is_open;
  816.     }
  817.     if (ecode < 0) {
  818.     if (bpp != old_bpp)
  819.         pm_set_bits_per_pixel(pmdev, old_bpp);
  820.     return ecode;
  821.     }
  822.     /* Hand off the change to the implementation. */
  823.     /* obtain mutex - to prevent gspmdrv from using bitmap */
  824.     /* while we change its size */
  825.     if (DosRequestMutexSem(pmdev->bmp_mutex, 20000) == ERROR_TIMEOUT)
  826.     fprintf(stderr, "pm_put_params: mutex timeout\n");
  827.     if (is_open && (old_bpp != bpp ||
  828.             dev->width != width || dev->height != height)
  829.     ) {
  830.     int ccode;
  831.  
  832.     ccode = pm_alloc_bitmap(pmdev, dev);
  833.     if (ccode < 0) {    /* Bad news!  Some of the other device parameters */
  834.         /* may have changed.  We don't handle this. */
  835.         /* This is ****** WRONG ******. */
  836.         dev->width = width;
  837.         dev->height = height;
  838.         pm_set_bits_per_pixel(pmdev, old_bpp);
  839.         pm_alloc_bitmap(pmdev, dev);
  840.         DosReleaseMutexSem(pmdev->bmp_mutex);
  841.         return ccode;
  842.     }
  843.     reopen = true;
  844.     }
  845.     pmdev->UpdateInterval = uii;
  846.     if (gsvs.data != 0) {
  847.     memcpy(pmdev->GSVIEW, gsvs.data, gsvs.size);
  848.     pmdev->GSVIEW[gsvs.size] = 0;
  849.     }
  850.     if (dev->is_open && reopen) {
  851.     /* need to update bitmap info header also */
  852.     pmdev->bmi->cx = dev->width;
  853.     pmdev->bmi->cy = dev->height;
  854.     /* update bit count and palette */
  855.     pmdev->bmi->cBitCount = dev->color_info.depth;
  856.     pmdev->bmi->cclrUsed = 1 << (pmdev->BitsPerPixel);
  857.     pmdev->bmi->cclrImportant = pmdev->nColors;
  858.     pm_makepalette(pmdev);
  859.     /* erase bitmap - before window gets redrawn */
  860.     (*dev_proc(dev, fill_rectangle)) (dev,
  861.                       0, 0, dev->width, dev->height,
  862.                    pm_map_rgb_color(dev, gx_max_color_value,
  863.                    gx_max_color_value, gx_max_color_value));
  864.     /* cause scroll bars to be redrawn */
  865.     /* need to signal gspmdrv that bitmap size has changed */
  866.     /* or perhaps gspmdrv can check if the bitmap size has */
  867.     /* before each use */
  868.  
  869. #ifdef __DLL__
  870.     if (pmdev->dll)
  871.         (*pgsdll_callback) (GSDLL_SIZE, (unsigned char *)dev,
  872.             (dev->width & 0xffff) + ((dev->height & 0xffff) << 16));
  873. #endif
  874.     }
  875.     /* release bmp mutex */
  876.     DosReleaseMutexSem(pmdev->bmp_mutex);
  877.     return 0;
  878. }
  879.  
  880. #ifdef __DLL__
  881. /* ------ DLL routines ------ */
  882. /* store at pbitmap the address of the bitmap */
  883. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  884. unsigned long GSDLLAPI
  885. gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap)
  886. {
  887.     gx_device *dev = (gx_device *) device;
  888.  
  889.     *pbitmap = (unsigned char *)(pmdev->bmi);
  890.     return 0;
  891. }
  892.  
  893. /* Lock the device (so it's size cannot be changed) if flag = TRUE */
  894. /* or unlock the device if flag = FALSE */
  895. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  896. int GSDLLAPI
  897. gsdll_lock_device(unsigned char *device, int flag)
  898. {
  899.     gx_device *dev = (gx_device *) device;
  900.     APIRET rc;
  901.  
  902.     if (flag)
  903.     rc = DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  904.     else
  905.     rc = DosReleaseMutexSem(pmdev->bmp_mutex);
  906.     return rc;
  907. }
  908.  
  909. #endif /* __DLL__ */
  910.  
  911. /* ------ Internal routines ------ */
  912.  
  913. #undef pmdev
  914.  
  915.  
  916. /* start gspmdrv.exe */
  917. private int
  918. pm_run_gspmdrv(gx_device_pm * pmdev)
  919. {
  920.     int ccode;
  921.     PCHAR pdrvname = "gspmdrv.exe";
  922.     CHAR error_message[256];
  923.     CHAR term_queue_name[128];
  924.     CHAR id[128];
  925.     CHAR arg[1024];
  926.     STARTDATA sdata;
  927.     APIRET rc;
  928.     PTIB pptib;
  929.     PPIB pppib;
  930.     CHAR progname[256];
  931.     PCHAR tail;
  932.  
  933.     sprintf(id, ID_NAME, pmdev->gspid, (ULONG) pmdev);
  934.  
  935.     /* Create termination queue - used to find out when gspmdrv terminates */
  936.     sprintf(term_queue_name, "\\QUEUES\\TERMQ_%s", id);
  937.     if (DosCreateQueue(&(pmdev->term_queue), QUE_FIFO, term_queue_name)) {
  938.     fprintf(stderr, "pm_run_gspmdrv: failed to create termination queue\n");
  939.     return gs_error_limitcheck;
  940.     }
  941.     /* get full path to gsos2.exe and hence path to gspmdrv.exe */
  942.     if ((rc = DosGetInfoBlocks(&pptib, &pppib)) != 0) {
  943.     fprintf(stderr, "pm_run_gspmdrv: Couldn't get module handle, rc = %d\n", rc);
  944.     return gs_error_limitcheck;
  945.     }
  946.     if ((rc = DosQueryModuleName(pppib->pib_hmte, sizeof(progname) - 1, progname)) != 0) {
  947.     fprintf(stderr, "pm_run_gspmdrv: Couldn't get module name, rc = %d\n", rc);
  948.     return gs_error_limitcheck;
  949.     }
  950.     if ((tail = strrchr(progname, '\\')) != (PCHAR) NULL) {
  951.     tail++;
  952.     *tail = '\0';
  953.     } else
  954.     tail = progname;
  955.     strcat(progname, pdrvname);
  956.  
  957.     /* Open the PM driver session gspmdrv.exe */
  958.     /* arguments are: */
  959.     /*  (1) -d (display) option */
  960.     /*  (2) id string */
  961.     sprintf(arg, "-d %s", id);
  962.  
  963.     /* because gspmdrv.exe is a different EXE type to gs.exe, 
  964.      * we must use start session not DosExecPgm() */
  965.     sdata.Length = sizeof(sdata);
  966.     sdata.Related = SSF_RELATED_CHILD;    /* to be a child  */
  967.     sdata.FgBg = SSF_FGBG_BACK;    /* start in background */
  968.     sdata.TraceOpt = 0;
  969.     sdata.PgmTitle = "Ghostscript PM driver session";
  970.     sdata.PgmName = progname;
  971.     sdata.PgmInputs = arg;
  972.     sdata.TermQ = term_queue_name;
  973.     sdata.Environment = pppib->pib_pchenv;    /* use Parent's environment */
  974.     sdata.InheritOpt = 0;    /* Can't inherit from parent because different sesison type */
  975.     sdata.SessionType = SSF_TYPE_DEFAULT;    /* default is PM */
  976.     sdata.IconFile = NULL;
  977.     sdata.PgmHandle = 0;
  978.     sdata.PgmControl = 0;
  979.     sdata.InitXPos = 0;
  980.     sdata.InitYPos = 0;
  981.     sdata.InitXSize = 0;
  982.     sdata.InitYSize = 0;
  983.     sdata.ObjectBuffer = error_message;
  984.     sdata.ObjectBuffLen = sizeof(error_message);
  985.  
  986.     rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  987.     if (rc == ERROR_FILE_NOT_FOUND) {
  988.     sdata.PgmName = pdrvname;
  989.     rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  990.     }
  991.     if (rc) {
  992.     fprintf(stderr, "pm_run_gspmdrv: failed to run %s, rc = %d\n", sdata.PgmName, rc);
  993.     fprintf(stderr, "pm_run_gspmdrv: error_message: %s\n", error_message);
  994.     return gs_error_limitcheck;
  995.     }
  996.     return 0;
  997.  
  998. }
  999.  
  1000. /* Allocate the backing bitmap. */
  1001. private int
  1002. pm_alloc_bitmap(gx_device_pm * pmdev, gx_device * param_dev)
  1003. {
  1004.     gx_device_memory mdev;
  1005.     byte *base;
  1006.     ulong data_size;
  1007.     uint ptr_size;
  1008.     uint pal_size;
  1009.     uint raster;
  1010.     ULONG rc;
  1011.     ULONG needed;
  1012.  
  1013.     /* Finish initializing the bitmap. */
  1014.  
  1015.     gs_make_mem_device(&mdev, gdev_mem_device_for_bits(pmdev->color_info.depth), 0, 0, (gx_device *) pmdev);
  1016.     mdev.width = param_dev->width;
  1017.     mdev.height = param_dev->height;
  1018.     /* BMP files need width rounded up so that a scan line is */
  1019.     /* a multiple of 4 bytes. */
  1020.     /* This is currently done by gdev_mem_raster(). */
  1021.     /* It may be better to do it here explicitly in case */
  1022.     /* gdev_mem_raster changes. */
  1023.     raster = gdev_mem_raster(&mdev);
  1024.     data_size = (ulong) raster *mdev.height;
  1025.  
  1026.     ptr_size = sizeof(byte **) * mdev.height;
  1027.     pal_size = pm_palette_size(pmdev);
  1028.     needed = pmdev->bmi->cbFix + pal_size + data_size + ptr_size;
  1029.     /* round up to page boundary */
  1030.     needed = (needed + MIN_COMMIT - 1) & (~(MIN_COMMIT - 1));
  1031.     if (needed > pmdev->committed) {
  1032.     /* commit more memory */
  1033.     if (rc = DosSetMem(pmdev->bitmap + pmdev->committed,
  1034.                needed - pmdev->committed,
  1035.                PAG_COMMIT | PAG_DEFAULT)) {
  1036.         fprintf(stderr, "No memory in pm_alloc_bitmap, rc = %d\n", rc);
  1037.         return gs_error_limitcheck;
  1038.     }
  1039.     pmdev->committed = needed;
  1040.     }
  1041.     /* Shared memory can't be decommitted */
  1042. #ifdef __DLL__
  1043.     if (pmdev->dll && (needed < pmdev->committed)) {
  1044.     /* decommit memory */
  1045.     if (rc = DosSetMem(pmdev->bitmap + needed,
  1046.                pmdev->committed - needed,
  1047.                PAG_DECOMMIT)) {
  1048.         fprintf(stderr, "Failed to decommit memory in pm_alloc_bitmap, rc = %d\n", rc);
  1049.         return gs_error_limitcheck;
  1050.     }
  1051.     pmdev->committed = needed;
  1052.     }
  1053. #endif
  1054.  
  1055.     /* Nothing can go wrong now.... */
  1056.     base = pmdev->bitmap + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1057.     pmdev->mdev = mdev;
  1058.     pmdev->mdev.base = (byte *) base;
  1059.     pmmproc(open_device) ((gx_device *) & pmdev->mdev);
  1060.     pmdev->bmi->cbImage = data_size;
  1061.     return 0;
  1062. }
  1063.  
  1064. private void
  1065. pm_makepalette(gx_device_pm * pmdev)
  1066. {
  1067.     int i, val;
  1068.     RGB2 *argb = (RGB2 *) ((PBYTE) pmdev->bmi + pmdev->bmi->cbFix);
  1069.  
  1070.     if (pmdev->BitsPerPixel > 8)
  1071.     return;            /* these don't use a palette */
  1072.  
  1073.     for (i = 0; i < pmdev->nColors; i++) {
  1074.     switch (pmdev->nColors) {
  1075.         case 64:
  1076.         /* colors are rrggbb */
  1077.         argb[i].bRed = ((i & 0x30) >> 4) * 85;
  1078.         argb[i].bGreen = ((i & 0xC) >> 2) * 85;
  1079.         argb[i].bBlue = (i & 3) * 85;
  1080.         argb[i].fcOptions = 0;
  1081.         /* zero unused entries */
  1082.         argb[i + 64].bRed = argb[i + 64].bGreen = argb[i + 64].bBlue = 0;
  1083.         argb[i + 64].fcOptions = 0;
  1084.         argb[i + 128].bRed = argb[i + 128].bGreen = argb[i + 128].bBlue = 0;
  1085.         argb[i + 128].fcOptions = 0;
  1086.         argb[i + 192].bRed = argb[i + 192].bGreen = argb[i + 192].bBlue = 0;
  1087.         argb[i + 192].fcOptions = 0;
  1088.         break;
  1089.         case 16:
  1090.         /* colors are irgb */
  1091.         val = (i & 8 ? 255 : 128);
  1092.         argb[i].bRed = i & 4 ? val : 0;
  1093.         argb[i].bGreen = i & 2 ? val : 0;
  1094.         argb[i].bBlue = i & 1 ? val : 0;
  1095.         if (i == 8) {    /* light gray */
  1096.             argb[i].bRed =
  1097.             argb[i].bGreen =
  1098.             argb[i].bBlue = 192;
  1099.             argb[i].fcOptions = 0;
  1100.         }
  1101.         break;
  1102.         case 2:
  1103.         argb[i].bRed =
  1104.             argb[i].bGreen =
  1105.             argb[i].bBlue = (i ? 255 : 0);
  1106.         argb[i].fcOptions = 0;
  1107.         break;
  1108.     }
  1109.     }
  1110. }
  1111.  
  1112.  
  1113. /* Cause display to be updated periodically */
  1114. private void
  1115. pm_update(gx_device_pm * pmdev)
  1116. {
  1117.     if (pmdev->updating)
  1118.     return;
  1119.     if (!pmdev->UpdateInterval)
  1120.     return;
  1121.     if (*pmdev->GSVIEW) {
  1122.     APIRET rc;
  1123.  
  1124.     rc = DosWriteQueue(pmdev->drv_queue, GS_UPDATING, 0, NULL, 0);
  1125.     if (rc)
  1126.         fprintf(stderr, "pm_update: DosWriteQueue error %d\n", rc);
  1127.     } else {
  1128.     DosStartTimer(pmdev->UpdateInterval, (HSEM) pmdev->sync_event,
  1129.               &pmdev->update_timer);
  1130.     }
  1131.     pmdev->updating = TRUE;
  1132. }
  1133.  
  1134. private uint
  1135. pm_set_bits_per_pixel(gx_device_pm * pmdev, int bpp)
  1136. {
  1137.     static const gx_device_color_info pm_24bit_color = dci_color(24, 255, 255);
  1138.     static const gx_device_color_info pm_8bit_color = dci_color(8, 31, 4);
  1139.     static const gx_device_color_info pm_4bit_color = dci_pc_4bit;
  1140.     static const gx_device_color_info pm_2color = dci_black_and_white;
  1141.     /* remember old anti_alias info */
  1142.     gx_device_anti_alias_info anti_alias = pmdev->color_info.anti_alias;
  1143.  
  1144.     switch (bpp) {
  1145.     case 24:
  1146.         pmdev->color_info = pm_24bit_color;
  1147.         pmdev->nColors = (1 << 24);
  1148.         break;
  1149.     case 8:
  1150.         /* use 64 static colors and 166 dynamic colors from 8 planes */
  1151.         pmdev->color_info = pm_8bit_color;
  1152.         pmdev->nColors = 64;
  1153.         break;
  1154.     case 4:
  1155.         pmdev->color_info = pm_4bit_color;
  1156.         pmdev->nColors = 16;
  1157.         break;
  1158.     case 1:
  1159.         pmdev->color_info = pm_2color;
  1160.         pmdev->nColors = 2;
  1161.         break;
  1162.     default:
  1163.         return (gs_error_rangecheck);
  1164.     }
  1165.     pmdev->BitsPerPixel = bpp;
  1166.     /* restore old anti_alias info */
  1167.     pmdev->color_info.anti_alias = anti_alias;
  1168.     return 0;
  1169. }
  1170.  
  1171. /* return length of BMP palette in bytes */
  1172. private uint
  1173. pm_palette_size(gx_device_pm * pmdev)
  1174. {
  1175.     switch (pmdev->color_info.depth) {
  1176.     case 24:
  1177.         return 0;
  1178.     case 8:
  1179.         return 256 * sizeof(RGB2);
  1180.     case 4:
  1181.         return 16 * sizeof(RGB2);
  1182.     }
  1183.     /* must be two color */
  1184.     return 2 * sizeof(RGB2);
  1185. }
  1186.  
  1187. /* This is used for testing */
  1188. /* Write out a BMP file to "out.bmp" */
  1189. private void
  1190. pm_write_bmp(gx_device_pm * pmdev)
  1191. {
  1192.     BITMAPFILEHEADER2 bmfh;
  1193.     uint bmfh_length = sizeof(BITMAPFILEHEADER2) - sizeof(BITMAPINFOHEADER2);
  1194.     uint length;        /* bitmap length */
  1195.     ULONG fh;            /* file handle */
  1196.     ULONG action;
  1197.     ULONG count;
  1198.  
  1199.     bmfh.usType = 0x4d42;    /* "BM" */
  1200.     length = pmdev->bmi->cbFix + pm_palette_size(pmdev)
  1201.     + ((gdev_mem_raster(&pmdev->mdev) * pmdev->mdev.height));
  1202.     bmfh.cbSize = bmfh_length + length;
  1203.     bmfh.xHotspot = bmfh.yHotspot = 0;
  1204.     bmfh.offBits = bmfh_length + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1205.     if (DosOpen("out.bmp",    /* filename */
  1206.         &fh,        /* pointer to handle */
  1207.         &action,    /* pointer to result */
  1208.         0,        /* initial length */
  1209.         FILE_NORMAL,    /* normal file */
  1210.         OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  1211.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE,
  1212.         0)) {
  1213.     fprintf(stderr, "error opening out.bmp\n");
  1214.     return;
  1215.     }
  1216.     if (DosWrite(fh, (PBYTE) & bmfh, bmfh_length, &count))
  1217.     fprintf(stderr, "error writing header for out.bmp\n");
  1218.     if (DosWrite(fh, pmdev->bitmap, length, &count))
  1219.     fprintf(stderr, "error writing out.bmp\n");
  1220.     if (DosClose(fh))
  1221.     fprintf(stderr, "error closing out.bmp\n");
  1222. }
  1223.